package com.jzo2o.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.BadRequestException;
import com.jzo2o.common.utils.UserContext;
import com.jzo2o.customer.constants.ServerConstance;
import com.jzo2o.customer.mapper.BankAccountMapper;
import com.jzo2o.customer.model.domain.BankAccount;
import com.jzo2o.customer.model.dto.request.BankAccountUpsertReqDTO;
import com.jzo2o.customer.model.dto.response.BankAccountResDTO;
import com.jzo2o.customer.service.IBankAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class BankAccountServiceImpl extends ServiceImpl<BankAccountMapper, BankAccount> implements IBankAccountService {
    @Autowired
    private BankAccountMapper bankAccountMapper;
    @Transactional(rollbackFor = Exception.class)
    @Override
    public BankAccountResDTO bankCount(BankAccountUpsertReqDTO dto) {
        if (dto.getType().equals(1)){
            dto.setType(ServerConstance.TYPE_INSTITUTION);

        }
        if (ObjectUtil.isEmpty(dto)) {
            throw new BadRequestException("参数不能为空");
        }
        //判断是否重复添加
        // 根据userId查询
        Long userId = UserContext.currentUserId();
       BankAccount bankAccounts = bankAccountMapper.selectOne(new LambdaQueryWrapper<BankAccount>().eq(BankAccount::getId, userId));
        if (ObjectUtil.isEmpty(bankAccounts)) {
            //不存在 进行新增操作
            BankAccount bankInfo = BeanUtil.toBean(dto, BankAccount.class);
            bankInfo.setId(userId);
            int insert = bankAccountMapper.insert(bankInfo);
            if (insert == 0) {
                throw new BadRequestException("您的信息有误,添加失败");
            }
            BankAccountResDTO bankAccountResDTO = BeanUtil.toBean(bankInfo, BankAccountResDTO.class);
            return bankAccountResDTO;
        }
        //已存在 执行修改
        BankAccount bankInfo = BeanUtil.toBean(dto, BankAccount.class);
        bankInfo.setId(userId);
        bankInfo.setPrimaryKey(bankAccounts.getPrimaryKey());
        bankAccountMapper.updateById(bankInfo);
        BankAccountResDTO bankAccountResDTO = BeanUtil.toBean(bankInfo, BankAccountResDTO.class);
        return bankAccountResDTO;

    }

    @Override
    public BankAccountResDTO queryAgencyBankAccount(Long id) {
        BankAccount bankAccount = lambdaQuery()
                .eq(BankAccount::getId, UserContext.currentUserId())
                .eq(BankAccount::getType, 3)
                .one();
        BankAccountResDTO bankAccountResDTO = new BankAccountResDTO();
        BeanUtil.copyProperties(bankAccount, bankAccountResDTO);
        return bankAccountResDTO;
    }

    /**
     * 5.3.1 新增或更新银行账号信息接口
     */
    @Override
    public void addBankAccount(BankAccountUpsertReqDTO bankAccountUpsertReqDTO) {
        //判断新增还是更新
        BankAccount bankAccount = lambdaQuery()
                .eq(BankAccount::getId, UserContext.currentUserId())
                .eq(BankAccount::getType, 2)
                .one();
        if (ObjectUtil.isEmpty(bankAccount)) {
            //新增
            bankAccount = new BankAccount();
        } else {
            //更新
        }
        BeanUtil.copyProperties(bankAccountUpsertReqDTO, bankAccount);
        bankAccount.setId(UserContext.currentUserId());
        if (bankAccount.getType() == 2) {
            boolean result = saveOrUpdate(bankAccount);
            if (!result) {
                throw new BadRequestException("设置银行账号信息失败");
            }
        } else {
            throw new BadRequestException("登录端口错误");
        }
    }



    /**
     * 5.3.2 获取当前用户银行账号接口
     */
    @Override
    public BankAccountResDTO queryWorkerBankAccount() {
        BankAccount bankAccount = lambdaQuery()
                .eq(BankAccount::getId, UserContext.currentUserId())
                .eq(BankAccount::getType, 2)
                .one();
        BankAccountResDTO bankAccountResDTO = new BankAccountResDTO();
        BeanUtil.copyProperties(bankAccount, bankAccountResDTO);
        return bankAccountResDTO;
    }
}

